import sys

from boot import (QWidget, SplitFluentWindow_SP, QIcon, FluentIcon, QApplication, FluentTranslator, Callable,
                  NavigationItemPosition, GlobalWidget, IMG_ROOT_DIR, Signal, Union, QCoreApplication, Qt, Signal, get_json_config,
                  QFrame, QVBoxLayout, QLineEdit)
from view.pages import PageSales, PageBill, PageCustomers, PageInfo
from view.widget.qq9_window_title import QQ9WindowTitle
from boot.font_family_load import font_family_load
from utils.lock import qq9_app_lock
from view.widget.qq9_general_wrapper import high_dpi
from view.widget.qq9_general_wrapper import popup
from utils.backup import backup_db
from view.widget.qq9_content_logo import QQ9ContentLogo
from view.widget.qq9_line_edit import QQ9LineEdit
from view.widget.qq9_button import QQ9RoundPushButton


__all__ = ["QQ9AppEngine", "MainWindow"]


@qq9_app_lock
@high_dpi
class QQ9AppEngine(object):

    def __init__(self, view_pointer: Callable):
        # 初始化
        # 初始化app
        self.app = QApplication([])
        # 加载字体
        font_family_load()
        # 加载翻译
        translator = FluentTranslator()
        self.app.installTranslator(translator)
        # 视图
        self.view = view_pointer()
        self.view.show()
        # 密码层
        self.password_overlay = PasswordOverlay(self.view)
        self.password_overlay.show()  # 显示遮罩层

        self.app.exec_()


@backup_db("blue2.0.db", get_json_config("root")["local_db"]["update_cycle"])
class MainWindow(SplitFluentWindow_SP):

    sgn_load_finished = Signal()
    sgn_tips_popup = Signal(str, bool)

    def __init__(self,
                 parent: Union[QWidget, None] = None,
                 ):
        super().__init__(parent)

        # 基础设置
        self.resize(1600, 1024)
        GlobalWidget.root = self

        # 初始化全局根窗口
        self.customers_navigation = None
        self.bill_navigation = None
        self.sales_navigation = None
        self.info_navigation = None

        # 初始化页面实例
        self.page_customers = PageCustomers(self)
        self.page_bill = PageBill(self)
        self.page_sales = PageSales(self)
        self.page_info = PageInfo(self)

        # 标题
        QQ9WindowTitle(self, icon_path=IMG_ROOT_DIR + "/favicon.ico", title="蓝盾记账2.0")

        # 导航初始化
        self.init_navigation()

        self.showMaximized()

        self.move(0, 0)

        # 绑定根窗体tips信号
        self.sgn_tips_popup.connect(self.tips_popup)

    def init_navigation(self) -> None:

        self.customers_navigation = self.addSubInterface(
            interface=self.page_customers,
            icon=FluentIcon.PEOPLE,
            text="客户管理"
        )

        self.bill_navigation = self.addSubInterface(
            interface=self.page_bill,
            icon=QIcon(IMG_ROOT_DIR + "/rmb.svg"),
            text="回款清单"
        )

        self.sales_navigation = self.addSubInterface(
            interface=self.page_sales,
            icon=FluentIcon.CALENDAR,
            text="发货清单"
        )

        self.info_navigation = self.addSubInterface(
            interface=self.page_info,
            icon=FluentIcon.INFO,
            text="关于我们",
            position=NavigationItemPosition.BOTTOM
        )

    def showEvent(self, event) -> None:
        super().showEvent(event)
        self.sgn_load_finished.emit()

    @popup
    def tips_popup(self, tips_text: str, status: bool):
        return tips_text, status


class PasswordOverlay(QFrame):
    def __init__(self, parent=None):
        super(PasswordOverlay, self).__init__(parent)

        # 设定宽高与主窗口一致
        self.setGeometry(parent.rect())
        self.setFixedWidth(1920)

        # 主布局
        self.main_v_layout = QVBoxLayout()
        self.main_v_layout.setContentsMargins(0, 0, 0, 0)
        self.main_v_layout.setSpacing(20)
        self.main_v_layout.setAlignment(Qt.AlignCenter)
        self.setLayout(self.main_v_layout)

        # logo
        self.logo_label = QQ9ContentLogo(icon_path=IMG_ROOT_DIR + "/支付安全.png")
        self.main_v_layout.addWidget(self.logo_label, alignment=Qt.AlignCenter)

        # 密码输入框
        self.password_edit = QQ9LineEdit()
        self.password_edit.setEchoMode(QLineEdit.Password)
        self.password_edit.setPlaceholderText("请输入密码")
        self.main_v_layout.addWidget(self.password_edit)

        # 登录按钮
        self.confire_button = QQ9RoundPushButton(icon_path=IMG_ROOT_DIR + "/右箭头.png", radius=20)
        self.main_v_layout.addWidget(self.confire_button, alignment=Qt.AlignCenter)

        # 系统退出按钮
        self.exit_button = QQ9RoundPushButton(parent=self, icon_path=IMG_ROOT_DIR + "/关闭.png", radius=20)
        self.exit_button.move(1920 - 50, 10)

        self.confire_button.sgn_clicked.connect(self.check_password)
        self.exit_button.sgn_clicked.connect(lambda: sys.exit(0))

        # 设置样式
        self._set_style_sheet()

    def _set_style_sheet(self):
        self.setStyleSheet("""
            PasswordOverlay {
                background-color: #FFFFFF;
            }
        """)

    # 验证密码
    def check_password(self):
        # 验证密码逻辑
        if self.password_edit.text() == "ml123456":
            self.close()
        else:
            self.password_edit.clear()
            self.password_edit.setPlaceholderText("密码错误")